package com.cheyunkeji.er.activity.auction;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cheyun.netsalev3.R;
import com.cheyunkeji.er.base.BaseActivity;
import com.cheyunkeji.er.bean.auction.CooperatorBean;
import com.cheyunkeji.er.view.SToast;
import com.cheyunkeji.er.view.TopBar;

import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import butterknife.BindView;
import butterknife.ButterKnife;

public class CooperatorProfileActivity extends BaseActivity {

    private static final int MY_PERMISSIONS_REQUEST_CALL_PHONE = 0x110;
    @BindView(R.id.v_topbar)
    TopBar vTopbar;
    @BindView(R.id.tv_cooperator_name)
    TextView tvCooperatorName;
    @BindView(R.id.tv_brand)
    TextView tvBrand;
    @BindView(R.id.tv_contact_person)
    TextView tvContactPerson;
    @BindView(R.id.tv_contact_number)
    TextView tvContactNumber;
    @BindView(R.id.tv_address)
    TextView tvAddress;
    @BindView(R.id.activity_cooperator_profile)
    LinearLayout activityCooperatorProfile;


    @Override
    protected void init() {
        setContentView(R.layout.er_activity_cooperator_profile);
        ButterKnife.bind(this);
    }

    @Override
    protected void initView() {

        vTopbar.setTitle("详情");
        vTopbar.setLeftBack();
        tvContactNumber.setOnClickListener(this);

    }

    @Override
    protected void initData() {

        if (null != getIntent().getSerializableExtra("COOPERATOR_INFO")) {
            CooperatorBean cooperatorBean = (CooperatorBean) getIntent().getSerializableExtra("COOPERATOR_INFO");

            if (null != cooperatorBean) {

                tvCooperatorName.setText(cooperatorBean.getTitle());
                tvBrand.setText(cooperatorBean.getBrand());
                tvContactNumber.setText(cooperatorBean.getMobile());
                tvAddress.setText(cooperatorBean.getRegion());
                tvContactPerson.setText(cooperatorBean.getRealname());

            }
        }
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);

        if (v.getId() == tvContactNumber.getId() && !TextUtils.isEmpty(tvContactNumber.getText().toString())) {

            if (ContextCompat.checkSelfPermission(this,
                    Manifest.permission.CALL_PHONE)
                    != PackageManager.PERMISSION_GRANTED) {

                ActivityCompat.requestPermissions(this,
                        new String[]{Manifest.permission.CALL_PHONE},
                        MY_PERMISSIONS_REQUEST_CALL_PHONE);
            } else {
                Intent intent = new Intent(Intent.ACTION_CALL);
                Uri data = Uri.parse("tel:" + tvContactNumber.getText().toString());
                intent.setData(data);
                startActivity(intent);
            }
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {

        switch (requestCode) {
            case MY_PERMISSIONS_REQUEST_CALL_PHONE:

                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    Intent intent = new Intent(Intent.ACTION_CALL);
                    Uri data = Uri.parse("tel:" + tvContactNumber.getText().toString());
                    intent.setData(data);
                    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                        return;
                    }
                    startActivity(intent);
                } else {
                    SToast.show("无法拨打电话");
                }

                break;

            default:
                break;
        }

        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    }

}
